package com.admin.service.impl;

import com.admin.entity.Admin;
import com.admin.entity.Apk;
import com.admin.mapper.AdminMapper;
import com.admin.mapper.ApkMapper;
import com.admin.service.IApkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author marisheng
 * @since 2019-11-21
 */
@Service
public class ApkServiceImpl extends ServiceImpl<ApkMapper, Apk> implements IApkService {


    @Autowired
    private ApkMapper apkMapper;
    @Autowired
    private AdminMapper adminMapper;
    /**
     * 查询普通Apk 以及根据条件查询
     * @param page
     * @param limit
     * @param id
     * @param map
     * @return
     */
    public IPage searchApkWithPageById(int page, int limit,int id,Map map)
    {
        Page p = new Page(page,limit);
        IPage iPage = apkMapper.searchApkWithPageById(p,id,map);
        return iPage;
    }


    /**
     * 新增普通apk应用
     * @param apk
     * @param username
     * @return
     */
    public int addApk(Apk apk, String username)
    {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_phone",username);
        Admin admin = adminMapper.selectOne(queryWrapper);
        if(admin!=null){
            apk.setApkAdmin(admin.getAdminId());
            if(selectApkMd5AndApkAdmin(apk.getApkMd5(),apk.getApkAdmin())!=null){
                return 0;
            }
        }else{
            return 0;
        }
        int insert = apkMapper.insert(apk);
        return insert;
    }

    /**
     * 查询普通apk根据用户，Md5进行比较
     * @param apkMd5
     * @param apkAdmin
     * @return
     */
    public Apk selectApkMd5AndApkAdmin (String apkMd5,int apkAdmin)
    {
        QueryWrapper q=new QueryWrapper();
        q.eq("apk_md5",apkMd5);
        q.eq("apk_admin",apkAdmin);
       Apk apk=apkMapper.selectOne(q);
       return apk;
    }

    /**
     * 查所有未审核apk
     * @param page
     * @param limit
     * @return
     */
    @Override
    public IPage apkShenhe(int page, int limit){
        Page p= new Page(page,limit);
        IPage re=apkMapper.apkshenhe(p);
        return  re;
    }

    /**
     * apk未审核的数量
     * @return
     */
    @Override
    public int apkWeishenheCount(){
        QueryWrapper q=new QueryWrapper();
        q.eq("apk_shenhe","未审核");
        int count=apkMapper.selectCount(q);
        return count;
    }
    public Admin findAdminByPhone(String username){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_phone",username);
        Admin admin = adminMapper.selectOne(queryWrapper);
        return admin;
    }

}
